package com.sykj.bsc.cl.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sykj.bsc.hyx.service.Bis_opinionService;
import com.sykj.bsc.jhy.entity.Bis_order_deposit;
import com.sykj.bsc.jhy.service.serviceImpl.Bis_nurse_projectServiceImpl;
import com.sykj.bsc.wsy.model.Bis_beauty_config;
import com.sykj.bsc.wsy.service.Bis_beauty_configService;
import com.sykj.bsc.zjj.bean.Bis_custom_user;
import com.sykj.bsc.zjj.service.Bis_shop_userService;
import com.sykj.bsc.zn.bean.Bis_beauty_record;
import com.sykj.bsc.zn.service.znServiceImpl;

/**
 * 支付Controller
 * @author Lenovo
 *
 */
@Controller
@RequestMapping("wechat")
public class PaymentController {
	//王舒雅 wsy
	@Autowired
	Bis_beauty_configService service;
	//张俊杰 zjj
	@Autowired
	private Bis_shop_userService bsus;
	//江海阳 jhy
	@Autowired 
	Bis_nurse_projectServiceImpl impl;
	//张诺 zn
	@Autowired
	znServiceImpl sus;
	//胡颖箫hyx
	@Autowired
	public Bis_opinionService bis_opinionService;
	
	/**
	 * 商品预约支付
	 */
	@RequestMapping("/orderpay")
	@ResponseBody
	public String orderpay(@RequestParam("project_id")String project_id,@RequestParam("shop_id")String shop_id,
			@RequestParam("subscribe_time")String subscribe_time,@RequestParam("beauty_price")String beauty_price,
			@RequestParam("order_content")String order_content,HttpSession session){
		//生成5位随机数
		int max=999999;
        int min=10000;
        Random random = new Random();
        Integer ran = random.nextInt(max)%(max-min+1) + min;
        String code1=ran.toString();
        int max1=999;
        int min1=10;
        Random random1 = new Random();
        Integer ran1 = random1.nextInt(max1)%(max1-min1+1) + min1;
        String code2=ran1.toString();
        String code=code1+code2;
        String code3="1706"+code1;
		Bis_custom_user ss=(Bis_custom_user) session.getAttribute("user");
		Map<String, Object> map=new HashMap<>();
		map.put("custom_user_id", ss.getCustom_user_id());
		map.put("project_id", project_id);
		map.put("shop_id", shop_id);
		map.put("service_time", subscribe_time);//服务时间
		map.put("extend_beauty_num", beauty_price);//订单金额
		map.put("order_id", code);
		map.put("order_content", order_content);
		map.put("beauty_price", beauty_price);//用户扣金额
		//向预约白添加预约信息
		this.bis_opinionService.saveBis_bisiness_order(map);
		//修改用户信息的颜值币
		this.bis_opinionService.updateBis_custom_user(map);
		//向订单定金信息表中添加信息
		map.put("deposit_id", code1);
		map.put("deposit_money", beauty_price);
		map.put("pay_way", 3);
		map.put("deposit_status", 1);
		this.bis_opinionService.saveBis_order_deposit(map);
		//向颜值流水信息表中添加信息
		map.put("beauty_num", beauty_price);//消费金额
		map.put("cdkey","");
		map.put("record_type", "2");
		map.put("record_id", code3);
		this.bis_opinionService.saveBis_beauty_record(map);
		return "1";
	}
	/**
	 * 取消预约
	 */
	@RequestMapping("cancellation")
	@ResponseBody
	public String cancellation(@RequestParam("order_id")String order_id,HttpSession session,HttpServletRequest request){
		Bis_custom_user ss=(Bis_custom_user) session.getAttribute("user");
		String custom_user_id=ss.getCustom_user_id();
		Map<String,Object> map=init1(request);
		map.put("order_id", order_id);
		map.put("order_status", 7);
		
		//查询订单定金信息表
		map.put("custom_user_id", custom_user_id);
		Bis_order_deposit bis_order_deposit=bis_opinionService.getBis_order_depositid(map);
		double beauty_num=bis_order_deposit.getDeposit_money();//颜值币数量
		//查询颜值流水信息表
		Bis_beauty_record bis_beauty_record=bis_opinionService.getBis_beauty_record(map);
		String shop_id=bis_beauty_record.getShop_id();//店铺编号
		
		//用户颜值币添加
		map.put("beauty_price", beauty_num);
		this.bis_opinionService.updateBis_custom_user1(map);
		
		//向颜值流水信息表中添加信息
		map.put("beauty_num", beauty_num);
		map.put("shop_id", shop_id);
		this.bis_opinionService.saveBis_beauty_record(map);
		
		//用户取消订单要对营业订单信息表进行修改
		this.bis_opinionService.updateBis_bisiness_order(map);
		
		//对订单定金信息表的状态进行修改
		map.put("deposit_status", "3");
		this.bis_opinionService.updateBis_order_deposit(map);
		
		return "1";
	}
	
	/**
	 * 对项目进行尾款结付
	 * @param request
	 * @return
	 */
	@RequestMapping("goPayOrder")
	@ResponseBody
	public String goPayOrder(@RequestParam("order_id")String order_id,@RequestParam("beauty_num")String beauty_num,
			@RequestParam("shop_id")String shop_id,HttpSession session,HttpServletRequest request){
		Bis_custom_user ss=(Bis_custom_user) session.getAttribute("user");
		String custom_user_id=ss.getCustom_user_id();
		Map<String,Object> map=init1(request);
		map.put("record_type", 2);
		map.put("order_id", order_id);
		map.put("custom_user_id", custom_user_id);
		map.put("shop_id", shop_id);
		map.put("beauty_num", beauty_num);//消费金额
		map.put("beauty_price", beauty_num);//用户扣金额
		//修改用户信息的颜值币
		this.bis_opinionService.updateBis_custom_user(map);
		//向颜值流水信息表中添加信息
		this.bis_opinionService.saveBis_beauty_record(map);
		//用户取消订单要对营业订单信息表进行修改
		map.put("order_status",4);
		this.bis_opinionService.updateBis_bisiness_order(map);
		return "1";
	}
	
	
	private Map init1(HttpServletRequest request){
		//生成5位随机数
		int max=999999;
        int min=10000;
        Random random = new Random();
        Integer ran = random.nextInt(max)%(max-min+1) + min;
        String code1=ran.toString();
        int max1=999;
        int min1=10;
        Random random1 = new Random();
        Integer ran1 = random1.nextInt(max1)%(max1-min1+1) + min1;
        String code3="1706"+code1;
		Map<String,Object> map=new HashMap<>();
		map.put("cdkey", "");
		map.put("record_id", code3);
		map.put("record_type", "4");
		return map;
	}
	
	
	/**
	 * 颜值币购买
	 */
	@RequestMapping("payment")
	@ResponseBody
	public String payment(HttpServletRequest request,HttpSession session){
		Bis_custom_user ss=(Bis_custom_user) session.getAttribute("user");
		String custom_user_id=ss.getCustom_user_id();
		Map<String, Object> map=init(request);
		
		//向颜值流水信息表中添加信息
        map.put("custom_user_id", custom_user_id);
		map.put("cdkey","");
		map.put("order_id","");
		map.put("record_type", "1");
		map.put("shop_id","");
		this.bis_opinionService.saveBis_beauty_record(map);
		//向现金流水信息表中添加信息
		map.put("pay_way", "1");
		this.bis_opinionService.saveBis_cash_record(map);
		//修改用户信息的颜值币
		this.bis_opinionService.updateBis_custom_user1(map);
		return "1";
	}
	
	private Map init(HttpServletRequest request){
		//生成5位随机数
		int max=999999;
        int min=10000;
        Random random = new Random();
        Integer ran = random.nextInt(max)%(max-min+1) + min;
        String code1=ran.toString();
        int max1=999;
        int min1=10;
        Random random1 = new Random();
        Integer ran1 = random1.nextInt(max1)%(max1-min1+1) + min1;
        String code3="1706"+code1;
		String config_id1=request.getParameter("config_id");//要够卖的颜值币id
		int config_id=Integer.parseInt(config_id1);
		Bis_beauty_config ss=service.findByConfig_idBis_beauty_config(config_id);
		System.out.println(ss.getBuy_num()+"::::::::::::"+ss.getGive_num());
		int buy_num=ss.getBuy_num();//要够卖的颜值币数量
		int give_num=ss.getGive_num();//要够卖的颜值币数量赠送的数量
		double money=buy_num*0.05;//要够卖的颜值币所需的钱
		System.err.println(money);
		int buy=give_num+buy_num;//要够卖的颜值币的总数量
		System.out.println(buy+":::");
		Map<String,Object> map=new HashMap<>();
		map.put("config_id", config_id);
		map.put("beauty_num", buy);
		map.put("buy_num", buy_num);
		map.put("money", money);
		map.put("record_id", code3);
		map.put("beauty_price", buy);//用户添加颜值币
		return map;
	}
	
	/**
	 * 抢礼包
	 * @throws ParseException 
	 */
	@RequestMapping("Gift_bag")
	@ResponseBody
	public String Gift_bag(HttpSession session,@RequestParam("bag_id")String bag_id,@RequestParam("total_price")String total_price,
			@RequestParam("soldout_time")String soldout_time,@RequestParam("as")String as,
			@RequestParam("project_id")String project_id) throws ParseException{
		System.out.println("::::::::::::::"+total_price);
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date date2=sdf.parse(soldout_time);
		int a=(int)(date2.getTime()-date.getTime());
		long days = a / ( 60 * 60 * 24*1000);
		System.err.println(days);
		if (date.getTime()<date2.getTime()) {
			//生成5位随机数
			int max=999999;
	        int min=10000;
	        Random random = new Random();
	        Integer ran = random.nextInt(max)%(max-min+1) + min;
	        String code1=ran.toString();
	        String code3="1706"+code1;
			Bis_custom_user ss=(Bis_custom_user) session.getAttribute("user");
			Map<String, Object> map=new HashMap<>();
			map.put("custom_user_id", ss.getCustom_user_id());
			map.put("bag_id", bag_id);
			map.put("beauty_num", total_price);
			map.put("record_id", code3);
			map.put("valid_day", days);
			map.put("beauty_price", total_price);//用户扣金额
			//修改用户信息的颜值币
			this.bis_opinionService.updateBis_custom_user(map);
			//礼包与项目信息表添加一条记录
			map.put("project_old_price", as);//项目原价
			map.put("project_new_price", total_price);//项目现价
			map.put("project_id", project_id);//项目编号
			map.put("project_num", 1);//项目数量
			//this.bis_opinionService.saveBis_bag_project(map);
			//向颜值流水信息表中添加信息
			map.put("cdkey","");
			map.put("order_id","");
			map.put("record_type", "3");
			/*map.put("beauty_num",total_price);*/
			map.put("shop_id","");
			this.bis_opinionService.saveBis_beauty_record(map);
			//用户购买以后修改礼包的数量
			map.put("remain_num",1);
			this.bis_opinionService.updateBis_nurse_bag(map);
			
			//往礼包表中添加一个礼包
			this.bis_opinionService.saveBis_bag_record(map);
			return "1";
		}
		
		return "0";
	}
	

}
